﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Serialization;

namespace fbasync
{
    [DataContract, Serializable]
    public class ActionLink
    {
        [DataMember(Name = "text"), XmlElement]
        public string text { get; set; }

        [DataMember(Name = "href"), XmlElement]
        public string href { get; set; }

        public virtual string ToJson()
        {
            var dict = new Dictionary<string, string>(2);
            dict.Add("text", text);
            dict.Add("href", href);
            return dict.ToJsonAssociativeArray();
        }

        public static ActionLink FromElement(XmlElement e)
        {
            if (null == e) return null;
            var text = e["text", FacebookConnection.FacebookNamespace];
            var href = e["href", FacebookConnection.FacebookNamespace];
            return new ActionLink { text = text.InnerText, href = href.InnerText };
        }

        public static IEnumerable<ActionLink> FromContainerElement(XmlElement e)
        {
            if (null == e) yield break;

            foreach (XmlNode c in e.ChildNodes)
                if (c.NodeType == XmlNodeType.Element)
                    yield return FromElement((XmlElement)c);
        }
    }
}
